<?php

namespace Library\Prizes;

use Library\CardCode;
use Think\Log;

/**
 * 
 *
 * @author pufan
 */
class Hongbao extends Prizes{
    
    public function __construct($desc) {
        parent::__construct($desc);
        $this->desc['type'] = Prizes::HONGBAO_RAND;
        if ($this->get_real_hb_amount() == TRUE) {
            $this->init_order();
        } else {
            $this->valid = FALSE; //奖品设置为无效,
        }
    }
    
    /*
     *  获取红包金额
     */
    private function get_real_hb_amount() {
        $money = $this->cardCode->get_hb_amount($this->get_order_attr('card_id'));
        if ($money) {
            $this->set_order_attr('money', $money);
            return TRUE;
        }
        return FALSE;
    }
    
    protected function save_order() {
        $this->prizes_order_model->startTrans(); 
        
        $data['card_type'] = 1;
        $data['card_id'] = $this->get_order_attr('card_id');
        $data['event_id'] = $this->get_order_attr('event_id');
        $data['openid'] = $this->get_order_attr('openid');
        $data['userid'] = $_SESSION['user_id'];
        $data['order_status'] = 0;
        $data['card_num'] = $this->get_order_attr('money');  //金额
        $data['winning_time'] = $this->winning_time;  //
        $data['sn_number'] = $this->sn_number;  //
        
        try {
            $result = $this->prizes_order_model->add($data);

            if ($result) { //统计中奖次数
                $result = $this->update_user_prizes_count($this->get_order_attr('card_id'), $this->get_order_attr('openid'));
            }
                  
            if ($result){
                $user_wallet = new \Library\UserWallet();
                $result = $user_wallet->add_money($this->get_order_attr('money'));
            } else {
                \Think\Log::record('order write mysql fail sql:', $this->prizes_order_model->getLastSql());
                \Think\Log::record('order write mysql fail arg:', json_encode($data));
            }
        } catch (\Exception $e) {
           $result = FALSE;
           \Think\Log::record('save_order catch Exception'.$e->getMessage());
        }

        if ($result){
            // 提交事务
            $this->prizes_order_model->commit(); 
            
            $hb_count = S($_SESSION['open_id'].':hb_count');
            S($_SESSION['open_id'].':hb_count', $hb_count+1, strtotime(date('Y-m-d',strtotime('+1 day'))) - time());
        }else{
           // 事务回滚
           $this->prizes_order_model->rollback(); 
           $this->cardCode->free_hb($this->get_order_attr('card_id'), $this->get_order_attr('money'));
           $this->valid = FALSE; //红包设置为无效,
           \Think\Log::record("prizes order fail");
        }
    }
    
    public function get_view_desc() {
        $data['money'] = $this->get_order_attr('money')/100; 
        $data['center_pic'] = $this->get_order_attr('center_pic'); 
        $data['center_url'] = $this->create_analytics_url();
        $data['congratulations'] = $this->get_order_attr('congratulations');
        $data['prizes_grade'] = $this->get_order_attr('prizes_grade');
        return $data;
    }
}
